﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DataModel;
using System.Diagnostics.CodeAnalysis;
using DataModel.Commands;
using ChangePasswordApp.Services.ChangePassword;
namespace ChangePasswordApp.ViewModel
{
    public class PasswordChangeViewModel:ViewModelBase
    {


        private string oldPassword;
        private string newPassword;
        private string confirmNewPassword;

        public string OldPassword
        {
            get { return oldPassword; }
            set
            {
                SetErrorMessageIf(String.IsNullOrEmpty(value), new ErrorMessage("OldPassword", "Old password can't be left blank."));
                if (oldPassword != value)
                {
                    oldPassword = value;
                    OnPropertyChanged("OldPassword");
                }
            }
        }
        public string NewPassword
        {
            get { return newPassword; }
            set
            {
                
                SetErrorMessageIf(String.IsNullOrEmpty(value), new ErrorMessage("NewPassword", "New password can't be left blank."));
                SetErrorMessageIf(!String.IsNullOrEmpty(value) && !String.IsNullOrEmpty(confirmNewPassword) && value != confirmNewPassword,
                        new ErrorMessage("ConfirmNewPassword", "The passwords entered do not match."));
                SetErrorMessageIf(!String.IsNullOrEmpty(value) && value == oldPassword,
                    new ErrorMessage("NewPassword", "The old password is the same as the new password, nothing will be changed."));
                SetErrorMessageIf(!IsStrongPassword(value), new ErrorMessage("NewPassword", PasswordRequirementDescription));

                if (newPassword != value)
                {
                    newPassword = value;
                    OnPropertyChanged("NewPassword");
                }
            }
        }
        public string ConfirmNewPassword
        {
            get { return confirmNewPassword; }
            set
            {
                SetErrorMessageIf(String.IsNullOrEmpty(value), new ErrorMessage("ConfirmNewPassword", "Confirm password can't be left blank."));
                SetErrorMessageIf(!String.IsNullOrEmpty(value) && newPassword != value,
                        new ErrorMessage("ConfirmNewPassword", "The passwords entered do not match."));
                if (confirmNewPassword != value)
                {
                    confirmNewPassword = value;
                    OnPropertyChanged("ConfirmNewPassword");
                }
            }
        }

        public ICommand CancelCommand { get { return new AlwaysOnCommand(cancel); } }
        public ICommand SaveCommand { get { return new ConditionCommand(save, new HasNetworkConnection(), new NoErrorsCondition(this)); } }

        #region Cancel and Save
        public void cancel()
        {
            oldPassword = newPassword = confirmNewPassword = "";
            OnPropertyChanged("OldPassword", "NewPassword", "ConfirmNewPassword");
            ClearErrorMessages();
        }
        public void save()
        {
            //Check none are empty
            if (SetErrorMessageIf(String.IsNullOrEmpty(OldPassword), new ErrorMessage("OldPassword", "Old password can't be left blank.")) |
                SetErrorMessageIf(String.IsNullOrEmpty(NewPassword), new ErrorMessage("NewPassword", "New password can't be left blank."))|
                SetErrorMessageIf(String.IsNullOrEmpty(ConfirmNewPassword), new ErrorMessage("ConfirmNewPassword", "Confirm password can't be left blank.")))
                return;
            //Check old and new passwords aren't the same
            if (SetErrorMessageIf(NewPassword == OldPassword,
                new ErrorMessage("NewPassword", "The old password is the same as the new password, nothing will be changed.")))
                return;
            //Check new password and confirm new password match
            if (SetErrorMessageIf(NewPassword != ConfirmNewPassword,
                        new ErrorMessage("ConfirmNewPassword", "The passwords entered do not match.")))
                return;

            //Save Data
            serviceClient.ChangePasswordCompleted += serviceClient_ChangePasswordCompleted;
            serviceClient.ChangePasswordAsync(OldPassword, NewPassword);
            SSCCMembership.BusyIndicator.Show("Please wait", HasProgress: true, EnableUserClose: false);
    
        }

        void serviceClient_ChangePasswordCompleted(object sender, ChangePasswordCompletedEventArgs e)
        {
            string Message = (e.Result)?"Success": "Incorrect Password";
            SSCCMembership.BusyIndicator.Show(() =>
                {
                    if(e.Result)
                        SSCCMembershipContent.NavigateCommand.NavigateTo("U_UserHome.UserHomePage");
                }, Message, HasProgress: false, SecondsTimeout: 3, EnableUserClose: true);
            serviceClient.ChangePasswordCompleted -= serviceClient_ChangePasswordCompleted;
        }



        ChangePasswordServiceClient serviceClient = ServiceConfig.NewChangePasswordClient();


        #endregion

        const string PasswordRequirementDescription = "The password must be at least 5 characters long";
        bool IsStrongPassword(string password)
        {
            return password.Length >= 5;
        }

    }
}
